package com.company.ems.dto;

import java.util.List;

public class PageDTO<T> {
    private List<T> data;
    private Integer total;
    private Integer page;
    private Integer pageSize;
    private Integer totalPages;

    public PageDTO() {
    }

    public PageDTO(List<T> data, Integer total, Integer page, Integer pageSize) {
        this.data = data;
        this.total = total;
        this.page = page;
        this.pageSize = pageSize;
        // 确保分页计算正确
        if (total != null && pageSize != null && pageSize > 0) {
            this.totalPages = (int) Math.ceil((double) total / pageSize);
        } else {
            this.totalPages = 0;
        }
    }

    // Getter和Setter方法
    public List<T> getData() { return data; }
    public void setData(List<T> data) { this.data = data; }

    public Integer getTotal() { return total; }
    public void setTotal(Integer total) {
        this.total = total;
        // 重新计算总页数
        if (this.pageSize != null && this.pageSize > 0) {
            this.totalPages = (int) Math.ceil((double) total / this.pageSize);
        }
    }

    public Integer getPage() { return page; }
    public void setPage(Integer page) { this.page = page; }

    public Integer getPageSize() { return pageSize; }
    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
        // 重新计算总页数
        if (this.total != null && pageSize > 0) {
            this.totalPages = (int) Math.ceil((double) this.total / pageSize);
        }
    }

    public Integer getTotalPages() {
        if (totalPages == null && total != null && pageSize != null && pageSize > 0) {
            return (int) Math.ceil((double) total / pageSize);
        }
        return totalPages;
    }

    public void setTotalPages(Integer totalPages) { this.totalPages = totalPages; }
}